package com.bw.zcb.mvp.view.people;

import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.zcb.R;
import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.view.BaseFragment;
import com.bw.zcb.mvp.contract.LREContract;
import com.bw.zcb.mvp.di.DaggerLREComponent;
import com.bw.zcb.mvp.di.LREModules;
import com.bw.zcb.mvp.model.HttpCode;
import com.bw.zcb.mvp.model.entity.RecommendEntity;
import com.bw.zcb.mvp.presenter.LREPresenter;
import com.bw.zcb.mvp.view.people.adapter.RecommendAdapter;
import com.bw.zcb.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends BaseFragment<LREPresenter> implements LREContract.ILREView, OnItemChildClickListener {


    private SmartRefreshLayout recommendSmart;
    private RecyclerView recommendRv;
    private RecommendAdapter recommendAdapter;
    private LinearLayoutManager manager;

    @Override
    public int bindLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView() {
        recommendSmart = (SmartRefreshLayout) f(R.id.recommend_smart);
        recommendRv = (RecyclerView) f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        recommendRv.setLayoutManager(manager);
        recommendAdapter = new RecommendAdapter();
        recommendAdapter.setOnItemChildClickListener(this);
        recommendRv.setAdapter(recommendAdapter);

    }

    @Override
    public void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build().injectRecommendFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof RecommendEntity){
            RecommendEntity recommendEntity = (RecommendEntity) entity;
            recommendAdapter.setNewInstance(recommendEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }


    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<RecommendEntity.Values> list = (List<RecommendEntity.Values>) adapter.getData();
        ToastUtils.showMsg("当前点击:"+list.get(position).news_title);
    }
}
